package com.imooc.newretail.common.config;

/**
 * 注意导入包是否齐全
 **/

import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Function;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * new-retail-teach
 * <p>
 * SwaggerConfig
 * </p>
 *
 * @author Hank
 * @since 2020-06-29
 */
@Configuration
@EnableSwagger2
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class SwaggerConfig {
    /**
     * 定义分隔符
     */
    private static final String SPLITOR = ";";

    @Bean
    public Docket createSysRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("基础平台")
                .apiInfo(apiInfo())
                // 根路径
                .pathMapping("/")
                .select()
                //设置包扫描路径
                .apis(RequestHandlerSelectors.basePackage("com.imooc.newretail.bpl.ctrl"))
//                .apis(basePackage("com.imooc.newretail.bpl.ctrl" + SPLITOR
//                        + "com.imooc.newretail.smartcabinet.ctrl" + SPLITOR))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket createDMSRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("智能柜模块")
                .apiInfo(new ApiInfoBuilder().title("新零售之智能设备API")
                        .contact(new Contact("Hank",
                                "blog.dingn.xyz",
                                "625575981@qq.com"))
                        .version("v1.0").description("API 描述").build())
                .pathMapping("/device")
                .select()
                .apis(basePackage("com.imooc.newretail.smartcabinet.ctrl"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 设置API 信息
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("新零售OpenAPI")
                //联系人
                .contact(new Contact("Hank", "blog.dingn.xyz", "625575981@qq.com"))
                .version("v1.0")
                //描述
                .description("API 描述")
                .build();
    }

    /**
     * 重写basePackage方法，使能够实现多包访问，复制贴上去
     *
     * @param basePackage
     * @return com.google.common.base.Predicate<springfox.documentation.RequestHandler>
     * @author teavamc
     * @date 2019/1/26
     */
    public static Predicate<RequestHandler> basePackage(final String basePackage) {
        return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true);
    }

    private static Function<Class<?>, Boolean> handlerPackage(final String basePackage) {
        return input -> {
            // 循环判断匹配
            for (String strPackage : basePackage.split(SPLITOR)) {
                boolean isMatch = input.getPackage().getName().startsWith(strPackage);
                if (isMatch) {
                    return true;
                }
            }
            return false;
        };
    }

    private static Optional<? extends Class<?>> declaringClass(RequestHandler input) {
        return Optional.fromNullable(input.declaringClass());
    }
}
